package com.siyu.quartz.task;


import io.minio.ListObjectsArgs;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.Result;
import io.minio.errors.MinioException;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Component("minioFileClearTask")
public class MinioFileClearTask {

    @Autowired
    private MinioClient minioClient;

    @Transactional
    public void clear() {
        try {
            // 列出存储桶中的所有对象
            Iterable<Result<Item>> myObjects = minioClient.listObjects(ListObjectsArgs.builder().bucket("siyu").prefix("lingshigeo/").build());

            for (Result<Item> item : myObjects) {
                Item object = item.get();
                // 删除每一个对象
                minioClient.removeObject(
                        RemoveObjectArgs.builder()
                                .bucket("siyu")
                                .object(object.objectName())
                                .build());

            }
            System.out.println("临时文件夹的文件清除!");

        } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
            e.printStackTrace();
        }
    }


}
